package com.tjsinfo.tjsoa.documentationflow.dangweishouwen.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.jdbc.core.RowMapper;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.ISpringDao;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.dao.ITDangweifawenNeirongDAO;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenFujian;
import com.tjsinfo.tjsoa.documentationflow.dangweifawen.vo.TDangweifawenNeirong;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.dao.ITDangweishouwenNeirongDAO;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.vo.TDangweishouwenFujian;
import com.tjsinfo.tjsoa.documentationflow.dangweishouwen.vo.TDangweishouwenNeirong;
//import com.tjsinfo.tjsoa.documentationflow.gonghuifawei.dao.ITGonghuifaweiNeirongDAO;
//import com.tjsinfo.tjsoa.documentationflow.gonghuifawei.vo.TGonghuifaweiFujian;
//import com.tjsinfo.tjsoa.documentationflow.gonghuifawei.vo.TGonghuifaweiNeirong;
//import com.tjsinfo.tjsoa.documentationflow.tanweifawen.dao.ITTuanweifaweiNeirongDAO;
//import com.tjsinfo.tjsoa.documentationflow.tanweifawen.vo.TTuanweifaweiFujian;
//import com.tjsinfo.tjsoa.documentationflow.tanweifawen.vo.TTuanweifaweiNeirong;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenFujian;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenNeirong;
//import com.tjsinfo.tjsoa.documentationflow.zuzhibufawen.dao.ITZuzhibufaweiNeirongDAO;
//import com.tjsinfo.tjsoa.documentationflow.zuzhibufawen.vo.TZuzhibufaweiNeirong;
import com.tjsinfo.tjsoa.message.vo.TjsMessage;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemPost;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class TDangweishouwenNeirongBO implements ITDangweishouwenNeirongBO	{
	private ITDangweishouwenNeirongDAO  dangweishouwenNeirongDAO;
	private IQbcDao qbcDao;
	private ISpringDao springDao;
	
	
	public ISpringDao getSpringDao() {
		return springDao;
	}

	public void setSpringDao(ISpringDao springDao) {
		this.springDao = springDao;
	}
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	

	

	public ITDangweishouwenNeirongDAO getDangweishouwenNeirongDAO() {
		return dangweishouwenNeirongDAO;
	}

	public void setDangweishouwenNeirongDAO(
			ITDangweishouwenNeirongDAO dangweishouwenNeirongDAO) {
		this.dangweishouwenNeirongDAO = dangweishouwenNeirongDAO;
	}

	public  String addTDangweishouweineirong(TDangweishouwenNeirong dangweishouwenNeirong,String qingbanren,List<Files> list,Integer userId) {
		// TODO Auto-generated method stub
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TDangweishouwenFujian dangweishouwenFujian = new TDangweishouwenFujian();
			dangweishouwenFujian.setFujianAddress(file.toFilePath);
			dangweishouwenFujian.setFujianName(file.getOldFileName());
			dangweishouwenFujian.setFujianZhengname(file.getNewFileName());
			dangweishouwenNeirong.addFujian(dangweishouwenFujian);
		}
		String id = dangweishouwenNeirongDAO.save(dangweishouwenNeirong);
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		flowid = flowTemplate.creatProcessInstance("办公室主任批办", qingbanren, "党委收文");
		flowTemplate.setVariable("登记修改", userId.toString(), flowid);
		flowTemplate.setVariable("流程名称", "党委收文", flowid);
		dangweishouwenNeirong =	selectOneById(id);
		dangweishouwenNeirong.setFlowid(flowid.intValue());
		updateTDangweishouwenneirong(dangweishouwenNeirong);

		return id;
		
	}

	
	public  String addTDangweishouweineirong(TDangweishouwenNeirong dangweishouwenNeirong,List<String> qingbanren,List<Files> list,Integer userId) {
		// TODO Auto-generated method stub
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Files file = (Files) iterator.next();
			TDangweishouwenFujian dangweishouwenFujian = new TDangweishouwenFujian();
			dangweishouwenFujian.setFujianAddress(file.toFilePath);
			dangweishouwenFujian.setFujianName(file.getOldFileName());
			dangweishouwenFujian.setFujianZhengname(file.getNewFileName());
			dangweishouwenNeirong.addFujian(dangweishouwenFujian);
		}
		String id = dangweishouwenNeirongDAO.save(dangweishouwenNeirong);
		WorkFlowTemplate flowTemplate = new WorkFlowTemplate();
		Long flowid = 0l;
		flowid = flowTemplate.creatProcessInstance("待阅", qingbanren, "党委收文","to 待阅");		
		flowTemplate.setVariable("流程名称", "党委收文", flowid);
		
		dangweishouwenNeirong =	selectOneById(id);
		dangweishouwenNeirong.setFlowid(flowid.intValue());
		updateTDangweishouwenneirong(dangweishouwenNeirong);

		return id;
		
	}
	
	
	public TDangweishouwenNeirong selectOneById(String id) {
		// TODO Auto-generated method stub
		return dangweishouwenNeirongDAO.findById(id);
	}

	public Boolean updateTDangweishouwenneirong(TDangweishouwenNeirong dangweishouwenNeirong){
		// TODO Auto-generated method stub
		try {
			dangweishouwenNeirongDAO.merge(dangweishouwenNeirong);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public List<TDangweishouwenNeirong> findByExample(
			TDangweishouwenNeirong dangweishouwenNeirong) {
		// TODO Auto-generated method stub
		List<TDangweishouwenNeirong> list = dangweishouwenNeirongDAO.findByExample(dangweishouwenNeirong);
		return list;
	}

	
	
	
	public Integer selectDangweishouwenTotalLine(){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TDangweishouwenNeirong.class);
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}
	
	
	/**
	 * 查询行政收文的总行数
	 * @return
	 */
	public Integer selectDangweishouwenTotalLine(String laiwenjihuan,String laiwenzihao ,String zhaiyao){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TDangweishouwenNeirong.class);
		Disjunction disjunctionOr = Restrictions.disjunction();
		
		if(laiwenjihuan != null && !"".equals(laiwenjihuan)){
			disjunctionOr.add(Restrictions.like("laiwenjiguan", "%"+laiwenjihuan+"%"));
		}
		if(laiwenzihao != null && !"".equals(laiwenzihao)){
			disjunctionOr.add(Restrictions.like("laiwenzihao", "%"+laiwenzihao+"%"));
		}
		if(zhaiyao != null && !"".equals(zhaiyao)){
			disjunctionOr.add(Restrictions.like("zhaiyou", "%"+zhaiyao+"%"));
		}
		detachedCriteria.add(disjunctionOr);
		return  qbcDao.getQueryNumber(detachedCriteria, "id");
	}
	
	
	public List<TDangweishouwenNeirong> selectDangweishouwenPage(Integer first,Integer max){
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweishouwenNeirong.class);
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("shouwenbianhao"));
		return qbcDao.findPageByCriteria(criteria,orderlist, max, first).getItems();
	}
	
	/**
	 * 查询党委收文分页
	 * @return
	 */
	public List<TDangweishouwenNeirong> selectDangweishouwenPage(Integer first,Integer max,String laiwenjihuan,String laiwenzihao ,String zhaiyao){
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweishouwenNeirong.class);
		Disjunction disjunctionOr = Restrictions.disjunction();
		
		if(laiwenjihuan != null && !"".equals(laiwenjihuan)){
			disjunctionOr.add(Restrictions.like("laiwenjiguan", "%"+laiwenjihuan+"%"));
		}
		if(laiwenzihao != null && !"".equals(laiwenzihao)){
			disjunctionOr.add(Restrictions.like("laiwenzihao", "%"+laiwenzihao+"%"));
		}
		if(zhaiyao != null && !"".equals(zhaiyao)){
			disjunctionOr.add(Restrictions.like("zhaiyou", "%"+zhaiyao+"%"));
		}
		criteria.add(disjunctionOr);
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("shouwenbianhao"));
		return qbcDao.findPageByCriteria(criteria,orderlist, max, first).getItems();
	}
	
	
	public Integer selectDangweishouwenTotalLineCriteria(DetachedCriteria criteria){
		return  qbcDao.getQueryNumber(criteria, "id");
	}
	

	public List<TDangweishouwenNeirong> selectDangweishouwenPageCriteria(DetachedCriteria criteria, Integer first,Integer max){
		List<Order> orderlist = new ArrayList<Order>();
		orderlist.add(Order.desc("shouwenbianhao"));
		return qbcDao.findPageByCriteria(criteria,orderlist, max, first).getItems();
	}
	
	
	public Integer selectMaxZongShouWenHaoByYear(String shouwentype){
		StringBuffer sql = new StringBuffer("SELECT top(1) shouwenbianhao FROM t_dangweishouwen_neirong");
		sql.append("\n WHERE shouwentype=?");
		sql.append("\n ORDER BY sortid DESC");
		List<String> tempList = springDao.querySQL(sql.toString(), new Object[]{shouwentype}, new RowMapper(){
			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getString("shouwenbianhao");
			}
		});
		Integer maxbianhao=1;
		if (tempList.size()>0){
			maxbianhao=Integer.parseInt(tempList.get(0))+1;   //收文编号
		}
		return maxbianhao;
	}
	
	public Boolean issamezongshouwenhao(String zongshouwenhao){
		DetachedCriteria criteria = DetachedCriteria.forClass(TDangweishouwenNeirong.class);
		criteria.add(Restrictions.eq("shouwenbianhao", zongshouwenhao));
		List<TDangweishouwenNeirong> list = qbcDao.getCriteria(criteria);
		if (list.size() >0) {
			return true;
		} else {
			return false;

		}
		
	}
	
	/**
	 * 删除收文
	 * @param 总收文文号

	 * @return
	 */
	public Boolean deleteDangWeiShouWei(TDangweishouwenNeirong dangweishouwenNeirong){
		try {
			dangweishouwenNeirongDAO.delete(dangweishouwenNeirong);
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return false;
		}
	}
	
	public List<TDangweishouwenNeirong> selectDownDangWeishouwenNeirong(Integer userId,String proname){
		String sql =  new String("EXEC selectdowndangweifawen @dataBaseId = ?,@subUserId = ?,@proname = ?");
		List<TDangweishouwenNeirong> countList = springDao.querySQL(sql, new Object[]{1,userId,proname}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TDangweishouwenNeirong xingzhengshouwenNeirong = new TDangweishouwenNeirong();
				xingzhengshouwenNeirong.setZhaiyou(arg0.getString("biaoti"));
				xingzhengshouwenNeirong.setLaiwenriqi(arg0.getDate("fawendatetime"));
				xingzhengshouwenNeirong.setLaiwenjiguan(arg0.getString("laiwendanwei"));
				xingzhengshouwenNeirong.setLaiwenzihao(arg0.getString("wenhaotype"));
				xingzhengshouwenNeirong.setDataBaseId(arg0.getInt("dataBaseId"));
				xingzhengshouwenNeirong.setId(arg0.getString("docid"));
				xingzhengshouwenNeirong.setTaskId(arg0.getLong("taskid"));
				return xingzhengshouwenNeirong;
			}
			
		});
		return countList;
	}
	
	public TDangweishouwenNeirong selectDownDangWeishouwenNeirongById(Integer dataBaseId,String id){
		String sql =  new String("EXEC selectDangWeiDownFaWenInfo @dataBase = ?,@id = ?");
		List<TDangweishouwenNeirong> countList = springDao.querySQL(sql, new Object[]{dataBaseId,id}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TDangweishouwenNeirong xingzhengshouwenNeirong = new TDangweishouwenNeirong();
				xingzhengshouwenNeirong.setZhaiyou(arg0.getString("biaoti"));
				xingzhengshouwenNeirong.setLaiwenriqi(arg0.getDate("fawendatetime"));
				xingzhengshouwenNeirong.setLaiwenjiguan(arg0.getString("laiwendanwei"));
				xingzhengshouwenNeirong.setLaiwenzihao(arg0.getString("wenhaotype"));
				xingzhengshouwenNeirong.setDataBaseId(arg0.getInt("dataBaseId"));
				xingzhengshouwenNeirong.setDocId(arg0.getString("docname"));
				return xingzhengshouwenNeirong;
			}
			
		});
		return countList.get(0);
	}
	
	public List<TDangweishouwenFujian>  selectDownDangWeishouwenFuJian(Integer dataBaseId,String id){
		String sql =  new String("EXEC selectDangWeiDownFaWenFuJian @dataBase = ?,@id = ?");
		List<TDangweishouwenFujian> countList = springDao.querySQL(sql, new Object[]{dataBaseId,id}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TDangweishouwenFujian xingzhengshouwenFujian = new TDangweishouwenFujian();
				xingzhengshouwenFujian.setFujianAddress(arg0.getString("fujian_address"));
				xingzhengshouwenFujian.setFujianName(arg0.getString("fujian_name"));
				xingzhengshouwenFujian.setFujianZhengname(arg0.getString("fujian_zhengname"));
				return xingzhengshouwenFujian;
			}
			
		});
		return countList;
	}
	
	public Boolean endTask(Integer dataBaseId,Integer id){
		String sql =  new String("EXEC endTask @dataBase = ?,@id = ?");
		try {
			springDao.commonUpdate(sql, new Object[]{dataBaseId,id});
		
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public List<String> selectAllUserId(){
		StringBuffer sql = new StringBuffer("SELECT userId FROM dbo.t_system_user");
		List<String> userIdList =  springDao.querySQL(sql.toString(), new Object[]{}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getString("userId");
			}
			
		});
		
		return userIdList;
	}
	
	public Integer isChuanYueCount(Integer userId, Integer flowId){
		StringBuffer sql = new StringBuffer("SELECT COUNT(*) AS taskcount FROM dbo.JBPM_TASKINSTANCE AS taskinstance   	WHERE taskinstance.ISOPEN_ = 1 AND taskinstance.NAME_  =  ? AND taskinstance.ACTORID_ = ? AND taskinstance.PROCINST_ = ? ");
		List<Integer> userIdList =  springDao.querySQL(sql.toString(), new Object[]{"传阅",userId,flowId}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getInt("taskcount");
			}
			
		});
		
		return userIdList.get(0);
	}
	
	public List<TDangweishouwenNeirong> selectDangWeiShouWenDaiBan(Integer userId){
		List<TDangweishouwenNeirong> list = new ArrayList<TDangweishouwenNeirong>();
		StringBuffer sql = new StringBuffer("EXEC dangweishouwendaiban  @userId = ?");
		list = springDao.querySQL(sql.toString(), new Object[]{userId}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				TDangweishouwenNeirong xingzhengshouwenNeirong = new TDangweishouwenNeirong();
				xingzhengshouwenNeirong.setId(arg0.getString("id"));
				xingzhengshouwenNeirong.setTaskId(arg0.getLong("ID_"));
				xingzhengshouwenNeirong.setDangqianhuanjie(arg0.getString("NAME_"));
				xingzhengshouwenNeirong.setZhaiyou(arg0.getString("zhaiyou"));
				return xingzhengshouwenNeirong;
			}
			
		});
		return list;
	
	}
	
	/**
	 * 查询流程上一个环节
	 * @param flowid
	 * @return
	 */
	public String selectTopTask(Integer flowid){
		List<String> list = new ArrayList<String>();
 		StringBuffer sql = new StringBuffer("SELECT TOP 1 task.NAME_ AS taskname FROM dbo.JBPM_LOG  AS LOG_ JOIN dbo.JBPM_TASKINSTANCE AS task ON LOG_.TASKINSTANCE_ = task.ID_ ");
		sql.append("WHERE LOG_.TOKEN_  = ? AND LOG_.TASKINSTANCE_ IS NOT NULL AND LOG_.TASKACTORID_ IS NULL ORDER BY task.ID_ DESC ");
		list = springDao.querySQL(sql.toString(), new Object[]{flowid}, new RowMapper(){

			public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				
				return arg0.getString("taskname");
			}
			
		});
		if (list.size() > 0 ) {
			return list.get(0);
		} else {
			return "传阅";
		}
	}
	
	public Boolean deleteTask(Integer taskId){
		try {
			StringBuffer sql = new StringBuffer("delete from  dbo.JBPM_LOG WHERE TASKINSTANCE_ = ?  ");
			springDao.commonUpdate(sql.toString(), new Object[]{taskId});
			 sql = new StringBuffer("delete from JBPM_TASKINSTANCE where ID_ = ? ");
			springDao.commonUpdate(sql.toString(), new Object[]{taskId});
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
}
